var fm_deleter;
var fm_filesToDelete;

function fm_deleteSelected() {
	fm_filesToDelete = new Array();
	for (item in fm_currentList.ls) {
		if (fm_currentList.ls[item].selected) {
			fm_filesToDelete.push(fm_currentList.ls[item].path);
		}
	}
	var content = document.createElement("div");
	content.textContent = _T("ItemsToDelete") + fm_filesToDelete.length + ". " + _T("SureQ");
	popup_showDialog(_T("Confirm"), content, "fm_confirmedDelete()", "");
}

function fm_confirmedDelete() {
	fm_deleter = new fm_Deleter(fm_currentList.file.path, fm_filesToDelete);
	setTimeout("fm_deleter.goBaby();", 100);
}

function fm_Deleter(_dir, _fileList) {
	this.fileList = _fileList;
	this.dir = _dir;
	this.fileName;
	this.total = this.fileList.length;
	this.taskProgress;
	this.xhr;

	this.goBaby = function() {
		var title = _T("DeletingFilesFrom") + fm_currentList.file.path;
		this.taskProgress = new tasks_Task(title, 1);
		this.xhr = new XMLHttpRequest();
		// xhr.addEventListener("progress", handleProgress, false);
		this.xhr.addEventListener("load", this.handleLoad, false);
		this.xhr.addEventListener("error", this.handleError, false);
		this.xhr.addEventListener("abort", this.handleAbort, false);
		this.xhr.deleter = this;
		main_encryptReq(this.xhr,"fm_delete",this.fileList);
	};

	this.handleLoad = function(_e) {
		var deleter = this.deleter;
		var report = main_readJSONResponse(this);
		if(!report){
			return;
		}
		var message = _T("Finished");
		if (report.errors > 0) {
			message = _T("Failed");
			alert(report.message);
		}
		deleter.taskProgress.update(
				[ [ deleter.fileList.length, report.deleted ] ], message);
		if (fm_requiredPath == deleter.dir) {
			fm_innerLs(fm_requiredPath);
		}
	};

	this.handleError = function(_e) {
		var deleter = this.deleter;
		deleter.taskProgress.update([ [ 2, 0 ] ], _T("Failed"));
		if (fm_requiredPath == deleter.dir) {
			fm_innerLs(fm_requiredPath);
		}
	};

	this.handleAbort = this.handleError;
	this.handleTimeout = this.handleError;

}
